//
//  InAppPurchaseComboTopView.swift
//  FrameProduct
//
//  Created by delegate on 2025/10/21.
//

import UIKit

class InAppPurchaseComboTopView: UIView {
    var isPackageType: Int = 1
    var packageTypeChangeValueBlock: ((_ packageType: Int) -> Void)?
    override init(frame: CGRect) {
        super.init(frame: frame)

        setupUI()
        
    }
    private func setupUI() {
        addSubviews(bgImageView)
        bgImageView.addSubviews(vipButton, SVIPButton, personalTailorButton, arrowImageView, lineImageView, collectionView)
        bgImageView.snp.makeConstraints { make in
            make.left.top.right.equalToSuperview()
            make.height.equalTo(232)
            make.bottom.equalToSuperview()
        }
        vipButton.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalToSuperview()
            make.height.equalTo(47)
        }
        SVIPButton.snp.makeConstraints { make in
            make.top.equalTo(vipButton.snp.top)
            make.height.equalTo(vipButton.snp.height)
            make.width.equalTo(vipButton.snp.width)
            make.left.equalTo(vipButton.snp.right)
        }
        personalTailorButton.snp.makeConstraints { make in
            make.top.equalTo(SVIPButton.snp.top)
            make.height.equalTo(SVIPButton.snp.height)
            make.width.equalTo(SVIPButton.snp.width)
            make.left.equalTo(SVIPButton.snp.right)
            make.right.equalToSuperview()
        }
        lineImageView.snp.makeConstraints { make in
            make.top.equalTo(vipButton.snp.bottom)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
            make.height.equalTo(4)
        }
        arrowImageView.snp.makeConstraints { make in
            make.centerX.equalTo(vipButton.snp.centerX)
            make.bottom.equalTo(lineImageView.snp.bottom)
            make.width.equalTo(14)
            make.height.equalTo(10)
        }
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(vipButton.snp.bottom).offset(0)
            make.bottom.equalTo(0)
            make.left.equalToSuperview()
            make.right.equalToSuperview()
        }
    }
    
    //=================================================================
    //                           事件处理
    //=================================================================
    // MARK: - 事件处理
    @objc private func vipButtonAction() {
        
        isPackageType = 1
        
        vipButton.isSelected = true
        SVIPButton.isSelected = false
        personalTailorButton.isSelected = false
        
        vipButton.titleLabel?.font = .lxs.font(size: 20)
        SVIPButton.titleLabel?.font = .lxs.font(size: 14)
        personalTailorButton.titleLabel?.font = .lxs.font(size: 14)
        
        arrowImageView.image = UIImage(named: "buy_vip_arrow_icon")
        lineImageView.image = UIImage(named: "buy_vip_line_img")
        arrowImageView.snp.remakeConstraints { make in
            make.centerX.equalTo(vipButton.snp.centerX)
            make.bottom.equalTo(lineImageView.snp.bottom)
            make.width.equalTo(14)
            make.height.equalTo(10)
        }
        
        collectionView.reloadData()
        packageTypeChangeValueBlock?(isPackageType)
    }
    @objc private func SVIPButtonAction() {
        
        isPackageType = 2
        
        vipButton.isSelected = false
        SVIPButton.isSelected = true
        personalTailorButton.isSelected = false
        
        vipButton.titleLabel?.font = .lxs.font(size: 14)
        SVIPButton.titleLabel?.font = .lxs.font(size: 20)
        personalTailorButton.titleLabel?.font = .lxs.font(size: 14)
        
        arrowImageView.image = UIImage(named: "buy_svip_arrow_icon")
        lineImageView.image = UIImage(named: "buy_svip_line_img")
        arrowImageView.snp.remakeConstraints { make in
            make.centerX.equalTo(SVIPButton.snp.centerX)
            make.bottom.equalTo(lineImageView.snp.bottom)
            make.width.equalTo(14)
            make.height.equalTo(10)
        }
        collectionView.reloadData()
        packageTypeChangeValueBlock?(isPackageType)
    }
    @objc private func personalTailorButtonAction() {
        
        isPackageType = 3
        
        vipButton.isSelected = false
        SVIPButton.isSelected = false
        personalTailorButton.isSelected = true
        
        vipButton.titleLabel?.font = .lxs.font(size: 14)
        SVIPButton.titleLabel?.font = .lxs.font(size: 14)
        personalTailorButton.titleLabel?.font = .lxs.font(size: 20)
        
        arrowImageView.image = UIImage(named: "buy_personal_arrow_icon")
        lineImageView.image = UIImage(named: "buy_personal_line_img")
        arrowImageView.snp.remakeConstraints { make in
            make.centerX.equalTo(personalTailorButton.snp.centerX)
            make.bottom.equalTo(lineImageView.snp.bottom)
            make.width.equalTo(14)
            make.height.equalTo(10)
        }
        collectionView.reloadData()
        packageTypeChangeValueBlock?(isPackageType)
    }
    //=================================================================
    //                            lazy
    //=================================================================
    // MARK: - lazy
    private lazy var bgImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_combo_top_bg_img")
        imageView.isUserInteractionEnabled = true
        return imageView
    }()
    
    private lazy var vipButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("VIP", for: .normal)
        button.setTitle("VIP", for: .selected)
        button.setTitleColor(.lxs.hex("#5C677D"), for: .normal)
        button.setTitleColor(.lxs.hex("#B5DAFF"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 20)
        button.addTarget(self, action: #selector(vipButtonAction), for: .touchUpInside)
        button.isSelected = true
        return button
    }()
    private lazy var SVIPButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("SVIP", for: .normal)
        button.setTitle("SVIP", for: .selected)
        button.setTitleColor(.lxs.hex("#5C677D"), for: .normal)
        button.setTitleColor(.lxs.hex("#F8E9BA"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 14)
        button.addTarget(self, action: #selector(SVIPButtonAction), for: .touchUpInside)
        button.isSelected = false
        return button
    }()
    private lazy var personalTailorButton: UIButton = {
        let button = UIButton(type: .custom)
        button.setTitle("私人订制", for: .normal)
        button.setTitle("私人订制", for: .selected)
        button.setTitleColor(.lxs.hex("#5C677D"), for: .normal)
        button.setTitleColor(.lxs.hex("#7EF5AA"), for: .selected)
        button.titleLabel?.font = .lxs.font(size: 14)
        button.addTarget(self, action: #selector(personalTailorButtonAction), for: .touchUpInside)
        button.isSelected = false
        return button
    }()
    private lazy var arrowImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_vip_arrow_icon")
        return imageView
    }()
    private lazy var lineImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.image = UIImage(named: "buy_vip_line_img")
        return imageView
    }()
    private lazy var collectionView: UICollectionView = {
        let flowLayout = UICollectionViewFlowLayout()
        flowLayout.itemSize = CGSize(width: 120, height: 144)
        flowLayout.sectionInset = UIEdgeInsets(top: 16, left: 20, bottom: 25, right: 20)
        flowLayout.minimumInteritemSpacing =  0
        flowLayout.minimumLineSpacing = 10
        flowLayout.scrollDirection = .horizontal
        let collection = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
        collection.delegate = self
        collection.dataSource = self
        collection.registerCellClass(InAppPurchaseComboTopViewCollectionViewCell.self)
        collection.backgroundColor = .clear
        return collection
    }()
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
extension InAppPurchaseComboTopView: UICollectionViewDelegate, UICollectionViewDataSource {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        10
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(InAppPurchaseComboTopViewCollectionViewCell.self, for: indexPath)
        cell.packageType = isPackageType
        cell.hasBgColor(isHave: indexPath.row == 0)
        return cell
    }
}
